package com.lyj.io.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 日期工具类
 *
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/8/10 16:58
 */
public class DateUtil {


    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYMMDD = "yyMMdd";

    public static String YYYYMMDD = "yyyyMMdd";

    public static String YYYYMM = "yyyyMM";

    public static String YYYY_MM_DDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYYMMDDHHMMSS2 = "yyyyMMdd HH:mm:ss";

    public static String YYYY_MM_DDHHMM = "yyyy-MM-dd HH:mm";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String HHMMSS = "HH:mm:ss";

    public static String HHMMSSSSS = "HHmmssSSS";

    public static String MMddHHmmssSSS = "MMddHHmmssSSS";

    public static String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    /**
     * 缺省月格式
     */
    public static final String DEFAULT_MONTH = "MONTH";

    /**
     * 缺省年格式
     */
    public static final String DEFAULT_YEAR = "YEAR";

    /**
     * 缺省日格式
     */
    public static final String DEFAULT_DATE = "DAY";

    /**
     * 缺省小时格式
     */
    public static final String DEFAULT_HOUR = "HOUR";

    /**
     * 缺省分钟格式
     */
    public static final String DEFAULT_MINUTE = "MINUTE";

    /**
     * 缺省秒格式
     */
    public static final String DEFAULT_SECOND = "SECOND";

    /**
     * 缺省秒格式
     */
    public static final String EXP_DATE = "20991231235959";

    /**
     * 格式化当前日期
     *
     * @param dateFormat-日期格式
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 17:12
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String format2String(String dateFormat) {
        return new SimpleDateFormat(dateFormat).format(new Date());
    }

    /**
     * 获取当前时间
     *
     * @param
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:13
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getCurrentDate() {
        return new Date();
    }

    ;

    /**
     * 格式化制定日期
     *
     * @param dateFormat-日期格式
     * @param theDate-指定的日期
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 17:13
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String format2String(String dateFormat, Date theDate) {
        if (theDate == null)
            return null;
        return new SimpleDateFormat(dateFormat).format(theDate);
    }

    /**
     * 字符串转日期格式
     *
     * @param dateString-日期字符串
     * @param dateFormat-日期格式
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getDateByString(String dateString, String dateFormat) throws ParseException {
        return new SimpleDateFormat(dateFormat).parse(dateString);
    }

    /**
     * 得到参数中天数的多少天之后的23:59:59的时间点
     *
     * @param millis-时间
     * @param amount-天数
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:22
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getDate4Amount(long millis, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        calendar.add(Calendar.DAY_OF_YEAR, 30);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取当月最后一天23:59:59
     *
     * @param millis
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:21
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getDate4LastDay(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 利用SimpleDateFormat的setLenient方法进行严格校验
     *
     * @param dateTime-时间字符串
     * @param dateFormat-时间格式
     * @return boolean
     * @author: liyajie
     * @date: 2021/8/10 17:20
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static boolean validDateTimeSimple(String dateTime, String dateFormat) {
        if (dateTime == null || dateFormat == null) {
            return false;
        }
        SimpleDateFormat df = new SimpleDateFormat(dateFormat);
        df.setLenient(false);//表示严格验证
        try {
            df.parse(dateTime);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    /**
     * 指定日期是否在开始时间和结束时间范围内
     *
     * @param nowTime-指定日期
     * @param startTime-开始日期
     * @param endTime-结束日期
     * @return boolean
     * @author: liyajie
     * @date: 2021/8/10 17:18
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar nowDate = Calendar.getInstance();
        nowDate.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (nowDate.after(begin)
                && nowDate.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 当前时间添加N月
     *
     * @param month-月数
     * @param format-当前时间
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 17:31
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String getAddMonth(int month, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, month);
        return sdf.format(ca.getTime());
    }

    /**
     * 当前时间添加N小时
     *
     * @param addHour-小时数
     * @param simpleDateFormat-时间格式
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 17:32
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String getAddHourTime(int addHour, SimpleDateFormat simpleDateFormat) {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.HOUR_OF_DAY, addHour);
        return simpleDateFormat.format(ca.getTime());
    }

    /**
     * 当前时间添加N分钟
     *
     * @param amount-分钟数
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 17:34
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String addMinutes(int amount, SimpleDateFormat simpleDateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, amount);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 功能：对于给定的时间增加天数/月数/年数后的日期,按指定格式输出
     *
     * @param date-要改变的日期
     * @param field-日期改变的字段，YEAR,MONTH,DAY
     * @param amount-改变量
     * @param strFormat-日期返回格式
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 17:28
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String getAddTime(String date, String field, int amount, String strFormat) {
        if (strFormat == null) {
            strFormat = YYYY_MM_DDHHMMSS;
        }
        Calendar rightNow = Calendar.getInstance();
        try {
            if (date != null && !"".equals(date.trim())) {
                rightNow.setTime(getDateByString(date, strFormat));
            }
            if (field == null) {
                return toString(rightNow.getTime(), strFormat);
            }
            rightNow.add(getInterval(field), amount);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return toString(rightNow.getTime(), strFormat);
    }

    /**
     * 获取时间间隔类型
     *
     * @param field-时间间隔类型
     * @return int-日历的时间间隔
     * @author: liyajie
     * @date: 2021/8/10 18:18
     * @exception:
     * @update:
     * @updatePerson:
     **/
    protected static int getInterval(String field) {
        String tmpField = field.toUpperCase();
        if (tmpField.equals(DEFAULT_YEAR)) {
            return Calendar.YEAR;
        } else if (tmpField.equals(DEFAULT_MONTH)) {
            return Calendar.MONTH;
        } else if (tmpField.equals(DEFAULT_DATE)) {
            return Calendar.DATE;
        } else if (DEFAULT_HOUR.equals(tmpField)) {
            return Calendar.HOUR;
        } else if (DEFAULT_MINUTE.equals(tmpField)) {
            return Calendar.MINUTE;
        } else {
            return Calendar.SECOND;
        }
    }

    /**
     * 将java.util.date型按照指定格式转为字符串
     *
     * @param date
     * @param format
     * @return java.lang.String
     * @author: liyajie
     * @date: 2021/8/10 18:17
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static String toString(Date date, String format) {
        return getSimpleDateFormat(format).format(date);
    }

    /**
     * 获取格式化对象
     *
     * @param strFormat-格式化字符串
     * @return java.text.SimpleDateFormat
     * @author: liyajie
     * @date: 2021/8/10 18:17
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static SimpleDateFormat getSimpleDateFormat(String strFormat) {
        if (strFormat != null && !"".equals(strFormat.trim())) {
            return new SimpleDateFormat(strFormat);
        } else {
            return new SimpleDateFormat();
        }
    }

    /**
     * 计算指定日期是该年中的第几周
     *
     * @param date-指定日期
     * @return java.lang.Integer
     * @author: liyajie
     * @date: 2021/8/10 17:38
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Integer getWeekOfYear(Date date) {
        if (null == date) {
            return 0;
        }
        return date2Calendar(date).get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 计算指定日期是该年中的第几天
     *
     * @param date-指定日期
     * @return java.lang.Integer
     * @author: liyajie
     * @date: 2021/8/10 17:38
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Integer getDayOfYear(Date date) {
        if (null == date) {
            return 0;
        }
        return date2Calendar(date).get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 计算指定日期是该月中的第几天
     *
     * @param date
     * @return java.lang.Integer
     * @author: liyajie
     * @date: 2021/8/10 17:38
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Integer getDayOfMonth(Date date) {
        if (null == date) {
            return 0;
        }
        return date2Calendar(date).get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 计算指定日期是月中的第几个星期
     *
     * @param date
     * @return java.lang.Integer
     * @author: liyajie
     * @date: 2021/8/10 17:39
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Integer getDayOfWeekInMonth(Date date) {
        if (null == date) {
            return 0;
        }
        return date2Calendar(date).get(Calendar.DAY_OF_WEEK_IN_MONTH);
    }

    /**
     * 计算指定日期是该周中的第几天(星期天1~~~星期六7)
     *
     * @param date-指定日期
     * @return java.lang.Integer
     * @author: liyajie
     * @date: 2021/8/10 17:39
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Integer getDayOfWeek(Date date) {
        if (null == date) {
            return 0;
        }
        return date2Calendar(date).get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 计算指定日期月份的最大天数
     *
     * @param date
     * @return int
     * @author: liyajie
     * @date: 2021/8/10 17:39
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static int getMaxDayInMonth(Date date) {
        if (date == null) {
            return 0;
        }
        return date2Calendar(date).getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * Date转换为Calendar
     *
     * @param date
     * @return java.util.Calendar
     * @author: liyajie
     * @date: 2021/8/10 18:20
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Calendar date2Calendar(Date date) {
        if (null == date) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 给指定日期添加或减去指定年
     *
     * @param date
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:39
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date addYear(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, amount);
        return calendar.getTime();
    }

    /**
     * 指定日期的年份
     *
     * @param date
     * @param year
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:40
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date setYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    /**
     * 指定日期的月份
     *
     * @param date
     * @param month
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:41
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date setMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 按Calendar的field 增加，返回对应日期。
     *
     * @param d
     * @param field
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 17:59
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date add(Date d, int field, int amount) {
        Date result = null;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(d);
            cal.add(field, amount);
            result = cal.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 给指定日期添加或减去指定周
     *
     * @param date
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:00
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date addWeek(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.WEEK_OF_MONTH, amount);
        return calendar.getTime();
    }

    /**
     * 给指定日期添加或减去指定天
     *
     * @param date
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:00
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date addDay(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, amount);
        return calendar.getTime();
    }

    /**
     * 指定日期的天数
     *
     * @param date
     * @param day
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:00
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date setDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 给指定日期添加或减去指定小时
     *
     * @param date
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:01
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date addHour(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, amount);
        return calendar.getTime();
    }

    /**
     * 指定日期的小时
     *
     * @param date
     * @param hour
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:01
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date setHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    /**
     * 给指定日期添加或减去指定分钟
     *
     * @param date
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:01
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date addMinute(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, amount);
        return calendar.getTime();
    }

    /**
     * 指定日期的分钟
     *
     * @param date
     * @param minute
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:02
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date setMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 给指定日期添加或减去指定秒
     *
     * @param date
     * @param amount
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:02
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date addSecond(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, amount);
        return calendar.getTime();
    }

    /**
     * 指定日期的秒
     *
     * @param date
     * @param second
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:02
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date setSecond(Date date, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }

    /**
     * 计算两个日期之间差的多少秒，如果日期firstDate在日期secondDate的后面则返回一个正数、如果日期firstDate在日期secondDate的前面则返回一个负数、两个日期相同返回0
     *
     * @param firstDate
     * @param secondDate
     * @return long
     * @author: liyajie
     * @date: 2021/8/10 18:05
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static long compareSecond(Date firstDate, Date secondDate) {
        long time1 = firstDate.getTime();
        long time2 = secondDate.getTime();
        return (time1 - time2) / (1000);
    }

    /**
     * 计算两个日期之间差的多少分钟，如果日期firstDate在日期secondDate的后面则返回一个正数、如果日期firstDate在日期secondDate的前面则返回一个负数、两个日期相同返回0
     *
     * @param firstDate
     * @param secondDate
     * @return long
     * @author: liyajie
     * @date: 2021/8/10 18:05
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static long compareMinute(Date firstDate, Date secondDate) {
        long time1 = firstDate.getTime();
        long time2 = secondDate.getTime();
        return (time1 - time2) / (1000 * 60);
    }

    /**
     * 计算两个日期之间差的多少小时，如果日期firstDate在日期secondDate的后面则返回一个正数、如果日期firstDate在日期secondDate的前面则返回一个负数、两个日期相同返回0
     *
     * @param firstDate
     * @param secondDate
     * @return long
     * @author: liyajie
     * @date: 2021/8/10 18:05
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static long compareHour(Date firstDate, Date secondDate) {
        long time1 = firstDate.getTime();
        long time2 = secondDate.getTime();
        return (time1 - time2) / (1000 * 60 * 60);
    }

    /**
     * 计算两个日期之间差的多少天，如果日期firstDate在日期secondDate的后面则返回一个正数、如果日期firstDate在日期secondDate的前面则返回一个负数、两个日期相同返回0
     *
     * @param firstDate
     * @param secondDate
     * @return long
     * @author: liyajie
     * @date: 2021/8/10 18:05
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static long compareDay(Date firstDate, Date secondDate) {
        long time1 = firstDate.getTime();
        long time2 = secondDate.getTime();
        return (time1 - time2) / (1000 * 60 * 60 * 24);
    }

    /**
     * 计算两个日期之间差的多少月,返回绝对值
     *
     * @param start
     * @param end
     * @return long
     * @author: liyajie
     * @date: 2021/8/10 18:06
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static long compareMonth(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);
        int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
        if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }

    /**
     * 获取指定日期对应的年份的第一天的日期
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:09
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);// 设为日期为今年的第1天
        return calendar.getTime();
    }

    /**
     * 获取指定日期对应的年份的最后一天的日期
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:09
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getLastDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);// 设为日期为今年的第1天
        calendar.add(Calendar.YEAR, 1);// 加上一年，到第二年的第一天
        calendar.add(Calendar.DATE, -1);// 减一天，今年的最后一天
        return calendar.getTime();
    }

    /**
     * 获取指定日期对应的季度的第一天的日期
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:09
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getFirstDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        if (0 == month || 1 == month || 2 == month) {
            calendar.set(Calendar.MONDAY, 0);
        } else if (3 == month || 4 == month || 5 == month) {
            calendar.set(Calendar.MONDAY, 3);
        } else if (6 == month || 7 == month || 8 == month) {
            calendar.set(Calendar.MONDAY, 6);
        } else {
            calendar.set(Calendar.MONDAY, 9);
        }
        return getFirstDayOfMonth(calendar.getTime());
    }

    /**
     * 获取指定日期对应的季度的最后一天的日期
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:10
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getLastDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        if (0 == month || 1 == month || 2 == month) {
            calendar.set(Calendar.MONDAY, 2);
        } else if (3 == month || 4 == month || 5 == month) {
            calendar.set(Calendar.MONDAY, 5);
        } else if (6 == month || 7 == month || 8 == month) {
            calendar.set(Calendar.MONDAY, 8);
        } else {
            calendar.set(Calendar.MONDAY, 11);
        }
        return getLastDayOfMonth(calendar.getTime());
    }

    /**
     * 获取指定日期对应的月份的第一天的日期
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:10
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);// 设为当前月的1号
        return calendar.getTime();
    }

    /**
     * 获取指定日期对应的月份的最后一天的日期
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:10
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);// 设为当前月的1号
        calendar.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
        calendar.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
        return calendar.getTime();
    }

    /**
     * 获取指定日期对应的周的第一天的日期(按中国习惯星期一作为一周的第一天)
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:10
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getFirstDayOfWeek(Date date) {
        int dayOfWeek = getDayOfWeek(date);
        if (2 == dayOfWeek) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        while (true) {
            calendar.add(Calendar.DATE, -1);
            if (2 == getDayOfWeek(calendar.getTime())) {
                break;
            }
        }
        return calendar.getTime();
    }

    /**
     * 获取指定日期对应的周的最后一天的日期(按中国习惯星期天作为一周的最后一天)
     *
     * @param date
     * @return java.util.Date
     * @author: liyajie
     * @date: 2021/8/10 18:11
     * @exception:
     * @update:
     * @updatePerson:
     **/
    public static Date getLastDayOfWeek(Date date) {
        int dayOfWeek = getDayOfWeek(date);
        if (1 == dayOfWeek) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        while (true) {
            calendar.add(Calendar.DATE, 1);
            if (1 == getDayOfWeek(calendar.getTime())) {
                break;
            }
        }
        return calendar.getTime();
    }
}
